<template>
  <div class="person">
    <h2>我是Person，现在一共有{{pictures.length}}张图</h2>
    <input type="text" placeholder="名字" v-model="name">
    <input type="text" placeholder="年龄" v-model="age">
    <button @click="handleAdd">添加</button>
    <ul>
      <li v-for="item in persons" :key="item.id">
        {{item.name}} -- {{item.age}}
      </li>
    </ul>
  </div>
</template>

<script>
  import { mapState } from 'vuex'
  import {v4} from 'uuid'
  export default {
    name:'Person',
    data() {
      return {
        name:'',
        age:''
      }
    },
    computed:{
      ...mapState(['persons','pictures'])
    },
    methods: {
      handleAdd(){
        // 获取输入并包装成一个合法的对象
        let obj = {id:v4(),name:this.name,age:this.age}
        // 联系action走添加流程
        this.$store.dispatch('addPerson',obj)
      }
    },
  }
</script>

<style scoped>
  .person {
    background-color: orange;
    border-radius: 10px;
    padding: 10px;
    box-shadow: 0 0 10px;
  }
  input {
    width: 100px;
    margin-right: 10px;
  }
</style>